package com.peas.platform.module.icims.budget.ysy.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.budget.project.dto.*;
import com.peas.platform.module.icims.budget.ysy.component.BudgetYsyGuangzhouClient;
import com.peas.platform.module.icims.budget.ysy.domain.BudgetYsyProjectTwoSyncDomain;
import com.peas.platform.module.icims.budget.ysy.dto.BudgetYsyLoginDto;
import com.peas.platform.module.icims.budget.ysy.dto.BudgetYsyProjectTwoSyncDto;
import com.peas.platform.module.icims.budget.ysy.service.BudgetYsyLoginService;
import com.peas.platform.module.icims.budget.ysy.service.BudgetYsyProjectTwoSyncService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BudgetYsyProjectTwoSyncServiceImpl extends BaseServiceImpl<BudgetYsyProjectTwoSyncDto> implements BudgetYsyProjectTwoSyncService {

    @Resource
    private BudgetYsyProjectTwoSyncDomain budgetYsyProjectTwoSyncDomain;
    @Resource
    private BudgetYsyLoginService ysyLoginService;
    @Resource
    private BudgetYsyGuangzhouClient ysyGuangzhouClient;

    @Override
    public BaseDomain<BudgetYsyProjectTwoSyncDto, ? extends BasePo> getDomain() {
        return budgetYsyProjectTwoSyncDomain;
    }

    @Transactional
    @Override
    public void syncBudgetProjectTwo(BudgetProjectTwoDto ptd, List<BudgetProjectTwoItemDto> ptids, List<BudgetProjectTwoYearDto> ptyds, List<BudgetProjectTwoKpiDto> ptkds, List<BudgetProjectTwoFileDto> ptfds) {
        BudgetYsyLoginDto dto;
        Page<BudgetYsyLoginDto> page = ysyLoginService.page(Page.of(1, 1));
        if (page == null || CollUtil.isEmpty(page.getRecords()) || DateUtil.offsetHour(page.getRecords().get(0).getCreateTime(), 2).isBefore(DateTime.now())) {
            dto = ysyLoginService.loginYsy();
        } else {
            dto = page.getRecords().get(0);
        }
        ysyGuangzhouClient.creation(dto, ptd, ptids, ptyds, ptkds, ptfds);
        // TODO 同步预算域
    }

}
